package com.dfrz.household.utils;/*
 * @创建者：黄苟苟
 * @创建时间：2021/8/18   14:46
 * @Description描述
 **/

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtils {
    /**
     * 添加cookie
     *
     * @param response
     * @param cookie
     */
    public static void addCookie(HttpServletResponse response, Cookie cookie) {
        if (cookie != null) {
            response.addCookie(cookie);
        }
    }

    /**
     * 添加相应信息的cookie
     *
     * @param response
     * @param cookieName
     * @param cookieValue
     * @param domain      cookie所在的子域
     * @param httpOnly
     * @param maxAge
     * @param path        设置cookie路径
     * @param secure      设置是否只允许Http访问
     */
    public static void addCookie(HttpServletResponse response, String cookieName, String cookieValue, String domain,
                                 boolean httpOnly, int maxAge, String path, boolean secure) {
        if (cookieName != null && !cookieName.equals("")) {
            if (cookieValue == null) {
                cookieValue = "";
                Cookie newCookie = new Cookie(cookieName, cookieValue);

                if (domain != null) {
                    newCookie.setDomain(domain);
                    newCookie.setHttpOnly(httpOnly);
                }

                if (maxAge > 0) {
                    newCookie.setMaxAge(maxAge);
                }

                if (path == null) {
                    newCookie.setPath("/");
                } else {
                    newCookie.setPath(path);

                    newCookie.setSecure(secure);

                    addCookie(response, newCookie);
                }
            }
        }
    }

    /**
     * @param response
     * @param cookieName
     * @param cookieValue
     * @param domain
     */
    public static void addCookie(HttpServletResponse response, String cookieName, String cookieValue, String domain) {
        addCookie(response, cookieName, cookieValue, domain, true,
                CookieConstantTable.COOKIE_MAX_AGE, "/", false);
    }

    /**
     * @param request
     * @param cookieName
     * @return
     */
    public static Cookie getCookie(HttpServletRequest request, String cookieName) {
        Cookie[] cookies = request.getCookies();

        if (cookies == null || cookieName == null || cookieName.equals("")) {
            return null;
        }

        for (Cookie c : cookies) {
            if (c.getName().equals(cookieName)) {
                return (Cookie) c;
            }
        }
        return null;
    }

    /**
     * @param request
     * @param cookieName
     * @return
     */
    public static String getCookieValue(HttpServletRequest request, String cookieName) {
        Cookie cookie = getCookie(request, cookieName);
        if (cookie == null) {
            return null;
        } else {
            return cookie.getValue();
        }
    }

    /**
     * 删除指定cookie
     *
     * @param response
     * @param cookie
     */

    public static void delCookie(HttpServletResponse response, Cookie cookie) {
        if (cookie != null) {
            cookie.setPath("/");
            cookie.setMaxAge(0);
            cookie.setValue(null);

            response.addCookie(cookie);
        }
    }

    /**
     * 删除指定的cookie值
     *
     * @param request
     * @param response
     * @param cookieName
     */
    public static void delCookie(HttpServletRequest request, HttpServletResponse response, String cookieName) {
        Cookie c = getCookie(request, cookieName);
        if (c != null && c.getName().equals(cookieName)) {
            delCookie(response, c);
        }
    }

    /**
     * 编辑修改cookie值
     * @param request
     * @param response
     * @param cookieName
     * @param cookieValue
     * @param domain
     */
    public static void editCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
                                  String cookieValue, String domain) {
        Cookie c = getCookie(request, cookieName);
        if (c != null && cookieName != null && !cookieName.equals("") && c.getName().equals(cookieName)) {
            addCookie(response, cookieName, cookieValue, domain);
        }
    }


}
